<template>
  <div class="Articleadd">
    <Head msg="文章发布"></Head>
    <input
      class="input-title"
      type="text"
      placeholder="请输入文章标题"
      v-model="title"
    />
    <textarea v-model="content" placeholder="请输入文章内容"></textarea>
    <div class="login-list">
      <div>封面图：</div>
    </div>
    <div class="article-pic">
      <cube-upload
        :value="value1"
        :action="action1"
        :simultaneous-uploads="1"
        :max="1"
        @file-removed="removed1"
        @files-added="filesAdded"
      />
    </div>
    <div class="login-list">
      <div>文章图片：</div>
    </div>
    <div class="article-pic">
      <cube-upload
        :value="value2"
        :action="action2"
        :simultaneous-uploads="1"
        @file-removed="removed2"
        @files-added="filesAdded"
      />
    </div>
    <div class="article-foot">
      <div class="article-other-btn" @click="topreview">预览</div>
      <div class="article-other-btn" @click="draftArticle">保存草稿</div>
      <div class="article-btn" @click="articlesubmit">
        {{ id ? '修改' : '发布' }}
      </div>
    </div>
    <div style="width: 100vw;height: 50px;"></div>
    //预览窗口
    <div v-if="previewshow" class="article-screen"></div>
    <div v-if="previewshow" class="article-preview">
      <cube-scroll ref="scroll">
        <img
          class="clear-preview"
          src="../../public/img/icons/icon-clear.png"
          @click="clearpreview"
        />
        <div class="article-main">
          <div class="article-list">
            <!--        <img src="../../public/img/icons/favicon-16x16.png" class="article-pic">-->
            <div class="article-content">
              <div class="article-title">{{ title }}</div>
              <div class="article-information">
                <div>作者：暂无</div>
                <!--            <div>评论：103</div>-->
                <div>发表时间：暂无</div>
              </div>
            </div>
          </div>
          <div class="article-body">
            <div v-html="content" class="detail-title"></div>
            <img
              v-for="(item, index) in files"
              class="detail-pic"
              :src="item"
              :key="index"
            />
          </div>
        </div>
      </cube-scroll>
    </div>
  </div>
</template>

<script lang="ts">
import { Component } from 'vue-property-decorator';
import Sence from '../sence/Sence';
// import HelloWorld from '@/components/HelloWorld.vue'; // @ is an alias to /src
import Head from '@/components/Head.vue';
@Component({
  components: {
    Head,
  },
})
export default class Articleadd extends Sence {
  public id: any = ''; //编辑文章id
  public value1: any = []; //封面图渲染数据
  public value2: any = []; //文章图渲染数据
  public title: string = '';
  public content: string = '';
  public file: string = ''; //封面图片提交数据
  public files: any = []; //文章图片提交数据
  public previewshow: boolean = false; //预览窗口控制
  public action1: any = {
    target: this.apiurl + 'api/v1/uploadImg',
    fileName: 'image',
    checkSuccess: this.uploadsuccess1,
  };
  public action2: any = {
    target: this.apiurl + 'api/v1/uploadImg',
    fileName: 'image',
    checkSuccess: this.uploadsuccess2,
  };

  public mounted(): void {
    if (this.$route.query.id) {
      this.id = this.$route.query.id;
      this.infoArticle();
    } else {
      this.getDraftArticle();
    }
  }

  //图片限制大小
  public filesAdded(files: any): void {
    let hasIgnore = false;
    const maxSize = 2 * 1024 * 1024; // 2M
    for (let k in files) {
      const file = files[k];
      if (file.size > maxSize) {
        file.ignore = true;
        hasIgnore = true;
      }
    }
    hasIgnore &&
      (<any>this)
        .$createToast({
          type: 'warn',
          time: 1500,
          txt: '上传失败，图片大于2M',
        })
        .show();
  }

  //封面图片上传成功回调
  public uploadsuccess1(res: any): any {
    console.log(res);
    this.file = res.data.name;
    return res.status == 1;
  }

  //文章图片上传成功回调
  public uploadsuccess2(res: any): any {
    this.files.push(res.data.name);
    return res.status == 1;
  }

  //删除封面图片
  public removed1(res: any): void {
    this.file = '';
  }

  //删除文章图片
  public removed2(res: any, k: number): void {
    console.log(k);
    for (let k in this.files) {
      if (this.files[k] == res.response.data.name) {
        this.files.splice(k, 1);
      }
    }
    console.log(res.response.data.name);
  }

  //发布审核
  public articlesubmit(): void {
    if (this.id) {
      this.saveArticle();
      return;
    }
    this.post('api/v1/releaseArticle', {
      title: this.title,
      content: this.content,
      files: JSON.stringify(this.files),
      photo: this.file,
    }).then((res: any) => {
      if (res.status == 1) {
        this.toast('发布成功');
        setTimeout(() => {
          (<any>this).$router.push('/article');
        }, 1500);
      }
    });
  }

  //文章详情
  public infoArticle(): void {
    this.post('api/v1/infoArticle', { article_id: this.id }).then(
      (res: any) => {
        this.title = res.data.info.title;
        this.content = res.data.info.content;
        this.file = res.data.info.photo;
        if (res.data.info.photo != '') {
          this.value1 = [
            {
              url: res.data.info.photo,
              response: { data: { name: res.data.info.photo } },
            },
          ];
        }
        if (res.data.info.files.length > 0) {
          for (let i = 0; i < res.data.info.files.length; i++) {
            this.value2 = [
              ...this.value2,
              {
                url: res.data.info.files[i].file,
                response: { data: { name: res.data.info.files[i].file } },
              },
            ];
            this.files = [...this.files, res.data.info.files[i].file];
          }
        }
      }
    );
  }

  //修改文章
  public saveArticle(): void {
    this.post('api/v1/saveArticle', {
      article_id: this.id,
      photo: this.file,
      title: this.title,
      content: this.content,
      files: JSON.stringify(this.files),
    }).then((res: any) => {
      this.toast('修改成功');
      setTimeout(() => {
        (<any>this).$router.push('/article');
      }, 1500);
    });
  }

  //保存草稿
  public draftArticle(): void {
    this.post('api/v1/draftArticle', {
      title: this.title,
      photo: this.file,
      content: this.content,
      files: JSON.stringify(this.files),
    }).then((res: any) => {
      this.toast('保存成功！');
    });
  }

  //获取草稿
  getDraftArticle(): void {
    this.post('api/v1/getDraftArticle', {}).then((res: any) => {
      this.title = res.data.info.title;
      this.content = res.data.info.content;
      this.file = res.data.info.photo;
      if (res.data.info.photo) {
        this.value1 = [
          {
            url: res.data.info.photo,
            response: { data: { name: res.data.info.photo } },
          },
        ];
      }
      if (res.data.info.files.length > 0) {
        for (let i = 0; i < res.data.info.files.length; i++) {
          this.value2 = [
            ...this.value2,
            {
              url: res.data.info.files[i].file,
              response: { data: { name: res.data.info.files[i].file } },
            },
          ];
          this.files = [...this.files, res.data.info.files[i].file];
        }
      }
    });
  }

  //预览
  public topreview(): void {
    this.previewshow = true;
  }

  //关闭
  public clearpreview(): void {
    this.previewshow = false;
  }
}
</script>

<style scoped lang="stylus">
input {
  height: 40px;
  line-height: 40px;
  width: 100vw;
  box-sizing: border-box;
  background: #f2f2f2;
  padding: 0 10px;
  font-size: 14px;
}

textarea {
  box-sizing: border-box;
  width: 100vw;
  margin-top: 10px;
  background: #f2f2f2;
  border: none;
  padding: 10px;
  font-size: 14px;
  height: 50vw;
}

.clear-preview {
  position: absolute;
  right: 0;
  top: 0;
  width: 25px;
  height: 25px;
  z-index: 99;
}

.article-screen {
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.3);
  position: fixed;
  top: 0;
}

.article-preview {
  height: 90vh;
  width: 80vw;
  position: fixed;
  margin: auto;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  border-radius: 8px;
  background: white;
  overflow: hidden;
  z-index: 99;
}

>>>.cube-upload-file-def, >>>.cube-upload-btn-def {
  width: 24vw;
  border: 18vw;
  border: 1px solid #ccc;
}

.login-title {
  height: 80px;
  text-align: center;
  background: #4876ff;
  color: white;
  font-weight: bold;
  font-size: 16px;
  line-height: 80px;
  margin-bottom: 30px;
}

.login-list {
  display: flex;
  flex-direction: row;
  align-items: center;
  margin-top: 20px;

  > div {
    width: 31vw;
    text-align: right;
  }
}

.article-pic {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  width: 80vw;
  margin: auto;
  margin-top: 20px;
}

.article-btn {
  background: #ff463f;
  color: white;
  width: 80px;
  font-weight: bold;
  padding: 5px 0;
  font-size: 15px;
  border-radius: 5px;
}

.article-foot {
  width: 100vw;
  box-sizing: border-box;
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  position: fixed;
  bottom: 0;
  height: 50px;
  align-items: center;
  background: #f2f2f2;
}

.article-other-btn {
  width: 80px;
  padding: 5px 0;
  border-radius: 5px;
  background: #ccc;
}

.article-main {
  .article-body {
    padding: 10px;

    /* border-bottom 3px solid #bbb */
    .detail-title {
      font-size: 14px;
      line-height: 18px;
      text-align: justify;
      margin-bottom: 10px;
    }

    .detail-pic {
      width: calc(80vw - 20px);
      height: 100%;
    }
  }

  .article-list {
    padding: 10px;
    background: #efeff4;
    display: flex;
    flex-direction: row;
    border-bottom: 3px solid #bbb;

    .article-content {
      width: 80vw;
      display: flex;
      flex-direction: column;
      justify-content: space-around;

      .article-title {
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
        text-overflow: ellipsis;
        text-align: left;
        line-height: 20px;
        font-weight: bold;
        font-size: 14px;
      }

      .article-information {
        margin-top: 5px;
        display: flex;
        flex-direction: row;

        div {
          margin-right: 15px;
          font-size: 12px;
          color: #808080;
          font-weight: bold;
        }
      }
    }
  }
}
</style>
